﻿using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.Alcs;
using KDS.Mes.Mes.Alcs.Dtos;
using KDS.Mes.Web.Models.Alcs;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class AlcsController : MesControllerBase
    {
        private readonly IAlcAppService _alcAppService;

        public AlcsController(IAlcAppService alcAppService)
        {
            _alcAppService = alcAppService;
        }

        public ActionResult Index()
        {
            //var model = new GetAlcInput { FilterText = Request.QueryString["filterText"] };
            return View();
        }

        [DontWrapResult]
        public JsonResult GetAllAlcs(int limit, int offset, string sortfiled, string sortway)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetAlcInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
            };
            var pagedTasks = _alcAppService.GetPagedAlcs(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Bom_Alcs_Create, AppPermissions.Pages_Bom_Alcs_Edit)]
        public async Task<PartialViewResult> CreateOrEditAlcModal(int? id)
        {
            var input = new NullableIdDto<int> { Id = id };

            var output = await _alcAppService.GetAlcForEditAsync(input);

            var viewModel = new CreateOrEditAlcModalViewModel(output);

            return PartialView("_CreateOrEditAlcModal", viewModel);
        }
    }
}